package com.qifenqian.turbot.module.adapter.restaurant

import android.app.Dialog
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.github.chrisbanes.photoview.PhotoView
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.entity.response.CommentEntity
import kotlinx.android.synthetic.main.item_comment_image.view.*
import org.jetbrains.anko.dip

/**
 * @author H
 * @createTime 2018/12/24 19:08
 * @description 用户评论中的图片适配器
 * @see
 * @since
 */
class CommentImageChildRvAdapter(context: Context, images: List<CommentEntity.Image>) : RecyclerView.Adapter<CommentImageChildRvAdapter.ImageViewHolder>() {

    private var mContext: Context? = null
    private var mImages: List<CommentEntity.Image>? = mutableListOf()

    init {
        mContext = context
        mImages = images
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ImageViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_comment_image, parent, false)
        return ImageViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mImages!!.size
    }

    override fun onBindViewHolder(holder: ImageViewHolder, i: Int) {
        val options = RequestOptions()
                .error(R.mipmap.ic_launcher)
                .override(mContext!!.dip(400),mContext!!.dip(500))
        Glide.with(mContext!!).load(mImages!![i].commentPhotoPath).apply(options).into(holder.itemView.iv_item_comment_image)
        holder.itemView.iv_item_comment_image.setOnClickListener {
            // TODO: 2019/1/9 这里可以创建ViewPager,滑动查看图片
            val dialog = Dialog(mContext!!)
            val iv = PhotoView(mContext)
            Glide.with(mContext!!).load(mImages!![i].commentPhotoPath).apply(options).into(iv)
            dialog.setContentView(iv)
            dialog.show()
        }
    }

    class ImageViewHolder(view: View) : RecyclerView.ViewHolder(view)

}
